<?php
########################################################################################
# Empresa :     Tecnopac Informatica;                                                  #
# Autor:        Deiverson Pacheco;                                                     #
# Data Criação: 10/12/2010;                                                            #
########################################################################################
session_start();
header("Cache-control: private");
include_once("../../includes/session.php");


//Linha para formatação de caracteres com acentuação
header("Content-Type: text/html; charset=ISO-8859-1",true);


//======================================================================================
//==> Include obrigatórias no script
//======================================================================================
include_once('../../includes/classes/class.TemplatePower.php');
include_once('../../includes/classes/class.Conexao.php');
include_once('../../includes/classes/class.Geral.php');
include_once('../../includes/classes/class.Paginador.php');
include_once('../../includes/classes/class.Component.php');

//======================================================================================
//==> Inicializar Classes
//======================================================================================
$not  = new Persist;
$data = new getData;
$cor  = new ColorTr;
$util = new Util;
$vld  = new ValidarDados;
$sql  = new SqlInjection;
$Paginacao = new Paginacao(6,3);
$cp = new Component();

//======================================================================================
//==> Instanciar TemplatePower para juntar o PHP + HTML
//======================================================================================
$tpl  = new TemplatePower('../../html/estoque/produtos.html');
$tpl->prepare();


//======================================================================================
//==> Recebe variaveis de pesquisa
//======================================================================================
$busca  = @$_GET['busca'];
$filtro = @$_GET['filtro'];
$pagina = @$_GET['pagina'] == '' ? 1 : $_GET['pagina'];

$sql_busca = '';

if($filtro == 'descricao'){
   $sql_busca = " and descricao like '%$busca%' ";
}else{
   $sql_busca = " and codigo like '%$busca%' ";
}

$cont   = 1;
$not->sql = "select id, codigo, descricao, pr_venda, loja1, loja2, loja3, deposito, estoque_min
from produtos
where ativo = 'Sim' $sql_busca order by descricao asc";
$tpl->assign('pag',$Paginacao->montaPaginacao($not->sql,'php/estoque/produtos.php?busca='.$busca.'&filtro='.$filtro,$pagina));
$not->sql = $Paginacao->montaQuery($not->sql,$pagina);
$cp->geraElementosTabela($not->sql);

if($not->ler()>0){
	while($not->ini()){
		$tpl->newBlock('LOOP');
		$tpl->assign('cont',$cont);
		$cont++;
		$tpl->assign('id', $not->codigo);
		$tpl->assign('nome', $not->descricao);
		$tpl->assign('pr_venda', number_format($not->pr_venda,2,',','.'));
		
		//posicao do estoque
		$estoque = $not->loja1 + $not->loja2 + $not->loja3 + $not->deposito;		

		if($estoque <= $not->estoque_min){
				$cores = 'red'; 			
		}else{
				$cores = 'blue'; 			
		}
		
		$tpl->assign('estoque', $estoque);
		$tpl->assign('cores', $cores);
		
		
		$tpl->assign('cor', $cor->altColor());

		//REFERENTE AOS LINKS DOS BOTOES EDITAR E EXCLUIR;
		$tpl->assign('par_id',$not->id);
		
	}
}
$tpl->goToBlock('_ROOT');

//======================================================================================
//==> Enviar página unificada PHP + HTML
//======================================================================================
$tpl->showUnAssigned( false );  // para debug. na hora de publicar, setar para false
$tpl->printToScreen();
?>